if not gopts.vals.xauthority:
gopts.vals.xauthority = get_xauthority()
+ gopts.is_xml = False
+
# Process remaining args as config variables.
for arg in args:
if '=' in arg:
if gopts.vals.config:
config = gopts.vals.config
else:
- gopts.load_defconfig()
- preprocess(gopts.vals)
- if not gopts.getopt('name') and gopts.getopt('defconfig'):
- gopts.setopt('name', os.path.basename(gopts.getopt('defconfig')))
- config = make_config(gopts.vals)
+ try:
+ gopts.load_defconfig()
+ preprocess(gopts.vals)
+ if not gopts.getopt('name') and gopts.getopt('defconfig'):
+ gopts.setopt('name', os.path.basename(gopts.getopt('defconfig')))
+ config = make_config(gopts.vals)
+ except XMLFileError, ex:
+ XMLFile = ex.getFile()
+ gopts.is_xml = True
+ config = ex.getFile()
return (gopts, config)
return str(gopts)
def main(argv):
+ is_xml = False
+
try:
(opts, config) = parseCommandLine(argv)
except StandardError, ex:
if not opts:
return
- if type(config) == str:
- try:
- config = sxp.parse(file(config))[0]
- except IOError, exn:
- raise OptionError("Cannot read file %s: %s" % (config, exn[1]))
-
- if serverType == SERVER_XEN_API:
- from xen.xm.xenapi_create import sxp2xml
- sxp2xml_inst = sxp2xml()
- doc = sxp2xml_inst.convert_sxp_to_xml(config, transient=True)
+ if not opts.is_xml:
+ if type(config) == str:
+ try:
+ config = sxp.parse(file(config))[0]
+ except IOError, exn:
+ raise OptionError("Cannot read file %s: %s" % (config, exn[1]))
+
+ if serverType == SERVER_XEN_API:
+ from xen.xm.xenapi_create import sxp2xml
+ sxp2xml_inst = sxp2xml()
+ doc = sxp2xml_inst.convert_sxp_to_xml(config, transient=True)
- if opts.vals.dryrun:
- SXPPrettyPrint.prettyprint(config)
+ if opts.vals.dryrun and not opts.is_xml:
+ SXPPrettyPrint.prettyprint(config)
- if opts.vals.xmldryrun and serverType == SERVER_XEN_API:
- from xml.dom.ext import PrettyPrint as XMLPrettyPrint
- XMLPrettyPrint(doc)
+ if opts.vals.xmldryrun and serverType == SERVER_XEN_API:
+ from xml.dom.ext import PrettyPrint as XMLPrettyPrint
+ XMLPrettyPrint(doc)
if opts.vals.dryrun or opts.vals.xmldryrun:
return
if serverType == SERVER_XEN_API:
from xen.xm.xenapi_create import xenapi_create
xenapi_create_inst = xenapi_create()
- vm_refs = xenapi_create_inst.create(document = doc)
+ if opts.is_xml:
+ vm_refs = xenapi_create_inst.create(filename = config)
+ else:
+ vm_refs = xenapi_create_inst.create(document = doc)
map(lambda vm_ref: server.xenapi.VM.start(vm_ref, 0), vm_refs)
- else:
+ elif not opts.is_xml:
if not create_security_check(config):
raise security.ACMError(
'Security Configuration prevents domain from starting')
import sys
import types
+
+
def _line_wrap(text, width = 70):
lines = []
current_line = ''
def __str__(self):
return self.message
+class XMLFileError(Exception):
+ """Thrown is input is an XML File"""
+ def __init__(self, XMLFile):
+ self.XMLFile = XMLFile
+ def __str__(self):
+ return "XMLFileError: %s" % self.XMLFile
+ def getFile(self):
+ return self.XMLFile
+
class Opt:
"""An individual option.
"""
p = os.path.join(os.path.curdir, p)
if os.path.exists(p):
self.info('Using config file "%s".' % p)
+
+ f = open(p)
+ is_xml = (f.read(1) == '<')
+ f.close()
+
+ if is_xml:
+ raise XMLFileError(p)
+
self.load(p, help)
break
else: